package com.lyf.core.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.ToastUtils;
import com.lyf.core.R;
import com.lyf.core.presenter.view.BaseView;
import com.lyf.core.ui.activity.BaseActivity;
import com.trello.rxlifecycle4.components.support.RxFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


public abstract class BaseFragment<VB extends ViewBinding> extends RxFragment implements BaseView, EasyPermissions.PermissionCallbacks {


    //权限请求码
    public static final int RC_PERMISSION_CODE = 99;

    protected boolean mIsInitData;

    protected VB mViewBinding;
    protected Context mContext = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mViewBinding = getViewBinding(inflater, container);
        this.mContext = inflater.getContext();
        return mViewBinding.getRoot();
    }

    protected abstract VB getViewBinding(LayoutInflater inflater, ViewGroup container);

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isRegisterEvent() && !EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        if (!isLazyLoad()) {
            fetchData();
        }
    }

    protected boolean isRegisterEvent() {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        fetchData();
    }

    private void fetchData() {
        if (mIsInitData)
            return;
        init();
        initEvent();
        initData();
        mIsInitData = true;
    }

    protected void init() {

    }

    protected void initEvent() {

    }

    protected void initData() {

    }

    /**
     * 是否懒加载
     */
    protected boolean isLazyLoad() {
        return false;
    }


    @Override
    public void showLoading() {
        showLoading("");
    }

    @Override
    public void showLoading(String title) {
        if (getActivity() == null) {
            return;
        }
        if (getActivity().isFinishing()) {
            return;
        }
        Activity activity = getActivity();
        if (activity instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity) activity;
            baseActivity.showLoading(title);
        }
    }

    @Override
    public void stopLoading() {
        if (getActivity() == null) {
            return;
        }
        if (getActivity().isFinishing()) {
            return;
        }
        Activity activity = getActivity();
        if (activity instanceof BaseActivity) {
            BaseActivity baseActivity = (BaseActivity) activity;
            baseActivity.stopLoading();
        }
    }


    @Override
    public void showMessage(String message) {
        ToastUtils.showShort(message);
    }

    @Override
    public void showMessage(int message) {
        ToastUtils.showShort(message);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this)
                    .setTitle(getString(R.string.permisssion_title_settings_dialog))
                    .setPositiveButton(getString(R.string.permission_button_setting))
                    .setNegativeButton(getString(R.string.permission_button_cancle))
                    .setRequestCode(RC_PERMISSION_CODE)
                    .build()
                    .show();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mIsInitData = false;
        mViewBinding = null;
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }


}
